#include <leveldb/db.h>
#include <iostream>
#include <ctime>


int main() {

    // Create a levelDB.
    leveldb::Options options;
    options.create_if_missing = false;
    std::string dbPath = "./data";
    leveldb::DB  *db;
    auto status = leveldb::DB::Open(options, dbPath.c_str(), &db);

    if (!status.ok()) {
        std::cerr << "Open Level DB Fail!" << std::endl;
        return 0;
    }

    auto writeEndTime = std::time(nullptr);
    std::localtime(&writeEndTime);

    // Read val for key.
    leveldb::ReadOptions readOptions;
    readOptions.fill_cache = false;
    for (int k = 0; k < 10; ++k) {
        for (int i = 0; i <= 10000; ++i) {
            std::string key = "name" + std::to_string(i);
            std::string val;
            status = db->Get(readOptions, key.c_str(), &val);
            if (!status.ok() || val != "value" + std::to_string(i)) {
                std::cerr << "Get Error!";
                return 0;
            }
        }
    }

    auto readEndTime = std::time(nullptr);
    std::localtime(&readEndTime);
    std::cout << "Read Consume Time: " << readEndTime - writeEndTime << std::endl;

    // Close DB.
    delete db;
    return 0;
}
